package edu.zqu.instructionalMatters.project.util;

import edu.zqu.instructionalMatters.project.VO.CurriculumVO;
import edu.zqu.instructionalMatters.project.entity.Calendar;
import edu.zqu.instructionalMatters.project.entity.Curriculum;
import edu.zqu.instructionalMatters.project.entity.Semester;
import edu.zqu.instructionalMatters.project.entity.TimeTable;
import edu.zqu.instructionalMatters.project.mapper.CalendarMapper;
import edu.zqu.instructionalMatters.project.mapper.SemesterMapper;
import edu.zqu.instructionalMatters.project.mapper.TimeTableMapper;
import edu.zqu.instructionalMatters.project.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author 何文东
 * @Date 2020/7/25 13:10
 * @Description: Curriculum 工具类
 * @Version 1.0
 */
@Component
public class CurriculumUtil {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private UserService userService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private CalendarMapper calendarMapper;
    @Autowired
    private TimeTableMapper timeTableMapper;
    @Autowired
    private SemesterMapper semesterMapper;
    @Autowired
    private InstructionClassService instructionClassService;
    @Autowired
    private FieldService fieldService;

    public CurriculumVO curriculumUtil(Curriculum curriculum) {

        CurriculumVO curriculumVO = new CurriculumVO();                                                                      //初始化 curriculumVO
        BeanUtils.copyProperties(curriculum, curriculumVO);                                                                   //对象复制赋值

        String collegeName = collegeService.getCollegeName(curriculum.getCollegeId());                                       //获取学校名称
        curriculumVO.setCollegeName(collegeName);                                                                            //设置学校名称

        String instructionClassName = instructionClassService.getInstructionClassName(curriculum.getInstructionClassId());   //获取授课班级名称
        curriculumVO.setInstructionClassName(instructionClassName);                                                          //设置授课班级名称

        String courseName = courseService.getCourseName(curriculum.getCourseId());                                           //获取课程名称
        curriculumVO.setCourseName(courseName);                                                                              //设置课程名称

        String fieldName = fieldService.getFieldName(curriculum.getFieldId());                                               //获取场地名称
        curriculumVO.setFieldName(fieldName);                                                                                //设置场地名称

        String teacherName = userService.getUserName(curriculum.getTeacherId());                                            //获取教师名称
        curriculumVO.setTeacherName(teacherName);                                                                            //设置教师名称

        Semester semester = semesterMapper.selectById(curriculum.getSemesterId());                                           //获取学期对象
        curriculumVO.setSemesterName(null);                                                                                  //设置学期名称null
        if (semester != null) {
            curriculumVO.setSemesterName(semester.getName());                                                                //设置学期名称
        }

        Calendar calendar = calendarMapper.selectById(curriculum.getCalendarId());                                          //获取校历对象
        curriculumVO.setCalendarDate(null);                                                                                 //设置校历日期null
        if (calendar != null) {
            curriculumVO.setCalendarDate(calendar.getTheDate().toString());                                                 //设置校历日期
        }

        TimeTable timeTable = timeTableMapper.selectById(curriculum.getTimeTableId());                                      //获取作息名称
        curriculumVO.setTimeTableName(null);                                                                                //设置作息表名称null
        if (timeTable != null) {
            curriculumVO.setTimeTableName(timeTable.getName());                                                             //设置作息表名称
        }

        return curriculumVO;                                                                                                //返回 curriculumVO
    }

}
